// services.ts
const app = getApp<IAppOption>()

interface Category {
  id: string;
  name: string;
  code: string;
}

interface Service {
  id: number;
  name: string;
  description: string;
  price: number;
  originalPrice: number;
  rating: number;
  category: string;
  tags: string[];
  image: string;
}

Component({
  data: {
    currentCategory: 'all',
    searchKeyword: '',
    categories: [] as Category[],
    services: [] as Service[],
    filteredServices: []
  },

  methods: {
    handlerGobackClick() {
      wx.navigateBack({
        delta: 1
      })
    },
    // 获取分类数据
    getCategories() {
      wx.request({
        url: `${app.globalData.apiConfig.baseUrl}/productCategory/list`,
        method: 'POST',
        success: (res: any) => {
          const resp = res.data
          if (resp.code === 200 && resp.data) {
            // 添加"全部"选项
            const allCategory = { id: 'all', name: '全部', code: 'all' }
            const categories = [allCategory, ...resp.data]
            this.setData({
              categories: categories
            })
          } else {
            console.error('获取分类数据失败:', res)
            // 如果接口失败，使用默认分类
            this.setData({
              categories: [
                { id: 'all', name: '全部', code: 'all' },
                { id: '1', name: '面部护理', code: 'facial' },
                { id: '2', name: '身体护理', code: 'body' },
                { id: '3', name: '头发护理', code: 'hair' },
                { id: '4', name: 'SPA按摩', code: 'spa' }
              ]
            })
          }
        },
        fail: (err: any) => {
          console.error('请求分类接口失败:', err)
          // 网络错误时使用默认分类
          this.setData({
            categories: [
              { id: 'all', name: '全部', code: 'all' },
              { id: '1', name: '面部护理', code: 'facial' },
              { id: '2', name: '身体护理', code: 'body' },
              { id: '3', name: '头发护理', code: 'hair' },
              { id: '4', name: 'SPA按摩', code: 'spa' }
            ]
          })
        }
      })
    },

    // 获取服务项目
    getServices(category?: string) {
      wx.request({
        url: `${app.globalData.apiConfig.baseUrl}/project/list`,
        method: 'POST',
        data:{
          category: category,
          status: 1
        },
        success: (res: any) => {
          const resp = res.data
          if (resp.code === 200 && resp.data) {
            this.setData({
              services: resp.data
            })
            this.filterServices()
          }
        },
        fail: (err: any) => {
          console.error('请求服务项目接口失败:', err)
        }
      })
    },

    // 切换分类
    switchCategory(e: any) {
      const category = e.currentTarget.dataset.category
      this.setData({ currentCategory: category })
      this.filterServices()
    },

    // 搜索服务
    onSearch(e: any) {
      const keyword = e.detail.value.toLowerCase()
      this.setData({ searchKeyword: keyword })
      this.filterServices()
    },

    // 过滤服务
    filterServices() {
      const { services, currentCategory, searchKeyword } = this.data
      let filtered = services

      // 按分类过滤
      if (currentCategory !== 'all') {
        filtered = filtered.filter(item => item.category === currentCategory)
      }

      // 按关键词搜索
      if (searchKeyword) {
        filtered = filtered.filter(item => 
          item.name.toLowerCase().includes(searchKeyword) ||
          item.description.toLowerCase().includes(searchKeyword) ||
          item.tags.some(tag => tag.toLowerCase().includes(searchKeyword))
        )
      }

      console.log('过滤后的服务:', filtered)

      this.setData({ filteredServices: filtered })
    },

    // 跳转到服务详情
    goToServiceDetail(e: any) {
      const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/service-detail/service-detail?id=${id}`
      })
    },

    // 跳转到预约页面
    goToBooking(e: any) {
      const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/service-detail/service-detail?id=${id}`
      })
    }
  },

  lifetimes: {
    attached() {
      this.getCategories()
      this.getServices()
    }
  }
})